#ifndef CARDREADER_H
#define CARDREADER_H

#include <QObject>
#include <QThread>
#include <QByteArray>

enum KeyType
{
    KEYA,
    KEYB
};

class CardReader : public QObject
{
    Q_OBJECT
public:
    explicit CardReader(QObject *parent = 0, int _dev = 0);

#ifdef __T10DEV
    long getDevId(void);
#endif

signals:
    void sendMsg(int mode, QByteArray data);
    void readReq(int mode);
    void writeReq(QByteArray data, QByteArray keyb, QByteArray blk3, int mode);

private slots:
    void doReadReq(int mode);
    void doWriteReq(QByteArray data, QByteArray keyb, QByteArray blk3, int mode);

#ifdef __A6DEV
private:
    void readFS(int mode);
    bool readBlockFS(int b, QByteArray &buf, QByteArray &key);
    void writeFS(QByteArray &data, QByteArray &key, QByteArray &blk3, int mode);
    bool writeBlockFS(int b, QByteArray &buf, QByteArray &key, int bkeya);
    void exitCard();
    bool openDevFS();
    bool authenFS(int b, QByteArray &key, int bkeya);
#endif

#ifdef __ZTDEV
private:
    bool openDevZT();
    bool authenZT(int b, QByteArray &key, int bkeya);
    void readZT(int mode);
    bool readBlockZT(int b, QByteArray &buf, QByteArray &key);
    void writeZT(QByteArray &data, QByteArray &key, QByteArray &blk3, int mode);
    bool writeBlockZT(int b, QByteArray &buf, QByteArray &key, int bkeya);
#endif

#ifdef __T10DEV
private:
    bool openDevT10();
    void readT10(int mode);
    bool readBlockT10(int b, QByteArray &buf, QByteArray &key);
    bool authenT10(int b, QByteArray &key, int type);
    void writeT10(QByteArray &data, QByteArray &key, QByteArray &blk3, int mode);
    bool writeBlockT10(int b, QByteArray &buf, QByteArray &key, int type);
    void exitCard();
    void enterCard();
#endif

private:
    bool openDev();
    bool scanCard();
    void read(int mode);
    bool readBlock(int b, QByteArray &buf);
    bool authen(int b, QByteArray &key, enum KeyType type);
    void write(QByteArray &data, QByteArray &key, QByteArray &blk3, int mode);
    bool writeBlock(int b, QByteArray &buf);
    void exitCard();
    void enterCard();
    void cleanup();

private:
    QThread worker;
    long fsdev;
    int ztdev;
    long t10dev;
    int dev;
    void* r6dev;
    void* hDev;
};

#endif // CARDREADER_H
